package net.hn.hnms.biz.sewage.service;

import cn.hutool.core.convert.Convert;
import net.hn.hnms.biz.sewage.dao.DomesticSewageAmmoniaNitrogenMapper;
import net.hn.hnms.biz.sewage.dao.DomesticSewageMapper;
import net.hn.hnms.biz.sewage.dao.DomesticSewageOdsMapper;
import net.hn.hnms.biz.sewage.dao.SewageIndexMapper;
import net.hn.hnms.biz.sewage.entity.DomesticSewageAmmoniaNitrogenEntity;
import net.hn.hnms.biz.sewage.entity.DomesticSewageMonthStatistics;
import net.hn.hnms.biz.sewage.entity.DomesticSewageOdsEntity;
import net.hn.hnms.biz.sewage.entity.DomesticSewageShiftEntity;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 智呈物联 nzw
 * @date 2022/9/23
 */
@Service
public class DomesticSewageService {
    private SewageIndexMapper sewageIndexMapper;

    private SewageIndexService sewageIndexService;

    private DomesticSewageMapper domesticSewageMapper;

    private DomesticSewageOdsMapper domesticSewageOdsMapper;

    private DomesticSewageAmmoniaNitrogenMapper domesticSewageAmmoniaNitrogenMapper;

    @Autowired
    public void setDomesticSewageMapper(DomesticSewageMapper domesticSewageMapper) {
        this.domesticSewageMapper = domesticSewageMapper;
    }

    @Autowired
    public void setDomesticSewageOdsMapper(DomesticSewageOdsMapper domesticSewageOdsMapper) {
        this.domesticSewageOdsMapper = domesticSewageOdsMapper;
    }

    @Autowired
    public void setDomesticSewageAmmoniaNitrogenMapper(DomesticSewageAmmoniaNitrogenMapper domesticSewageAmmoniaNitrogenMapper) {
        this.domesticSewageAmmoniaNitrogenMapper = domesticSewageAmmoniaNitrogenMapper;
    }

    @Autowired
    public void setSewageIndexMapper(SewageIndexMapper sewageIndexMapper) {
        this.sewageIndexMapper = sewageIndexMapper;
    }

    @Autowired
    public void setSewageIndexService(SewageIndexService sewageIndexService) {
        this.sewageIndexService = sewageIndexService;
    }

    public List<DomesticSewageShiftEntity> List(Long indexId){
        List<DomesticSewageShiftEntity> list = domesticSewageMapper.findAllByIndexId(indexId);
        for (DomesticSewageShiftEntity domesticSewageShiftEntity:list){
            domesticSewageShiftEntity.setDomesticSewageOdsEntities(domesticSewageOdsMapper.findAllByShiftId(domesticSewageShiftEntity.getId()));
            domesticSewageShiftEntity.setDomesticSewageAmmoniaNitrogenEntities(domesticSewageAmmoniaNitrogenMapper.findAllByShiftId(domesticSewageShiftEntity.getId()));
        }
        return list;
    }

    @Transactional
    public void save(DomesticSewageShiftEntity domesticSewageShiftEntity){
        Long indexId = domesticSewageShiftEntity.getIndexId();
        if (indexId!=null){
            List<DomesticSewageShiftEntity> list = domesticSewageMapper.findAllByIndexId(indexId);
            Long totalSewageTreatment = 0l;
            Long totalOverflow = 0l;
            Long totalReuse = 0l;
            for (DomesticSewageShiftEntity d:list){
                if (d.getId()==domesticSewageShiftEntity.getId()){
                    continue;
                }
                totalSewageTreatment += Long.parseLong(d.getSewageTreatment());
                totalOverflow += Long.parseLong(d.getOverflow());
                totalReuse += Long.parseLong(d.getReuse());
            }
            totalSewageTreatment += Long.parseLong(domesticSewageShiftEntity.getSewageTreatment());
            totalOverflow += Long.parseLong(domesticSewageShiftEntity.getOverflow());
            totalReuse += Long.parseLong(domesticSewageShiftEntity.getReuse());
            for (DomesticSewageShiftEntity d:list){
                if (d.getId()==domesticSewageShiftEntity.getId()){
                    continue;
                }
                d.setTotalSewageTreatment(totalSewageTreatment+"");
                d.setTotalOverflow(totalOverflow+"");
                d.setTotalReuse(totalReuse+"");
                domesticSewageMapper.save(d);
            }
            domesticSewageShiftEntity.setTotalSewageTreatment(totalSewageTreatment+"");
            domesticSewageShiftEntity.setTotalOverflow(totalOverflow+"");
            domesticSewageShiftEntity.setTotalReuse(totalReuse+"");
        }

        List<DomesticSewageOdsEntity> odsList = domesticSewageShiftEntity.getDomesticSewageOdsEntities();
        List<DomesticSewageAmmoniaNitrogenEntity> ammList = domesticSewageShiftEntity.getDomesticSewageAmmoniaNitrogenEntities();
//        List<Map<String,String>> users = domesticSewageShiftEntity.getUser();
//        Map<String,String> map = PersonOnDuty.user(users);
//        domesticSewageShiftEntity.setPersonOnDuty(map.get("userName"));
//        domesticSewageShiftEntity.setPersonOnDutyCode(map.get("userCode"));
        domesticSewageShiftEntity = domesticSewageMapper.save(domesticSewageShiftEntity);
        //更新总体表
        sewageIndexService.updateDomesticSewage(domesticSewageShiftEntity);

        List<DomesticSewageOdsEntity> oldOdsList = domesticSewageOdsMapper.findAllByShiftId(domesticSewageShiftEntity.getId());
        domesticSewageOdsMapper.deleteAll(oldOdsList);
        if (odsList!=null) {
            for (DomesticSewageOdsEntity ino : odsList) {
                ino.setShiftId(domesticSewageShiftEntity.getId());
                domesticSewageOdsMapper.save(ino);
            }
        }
        List<DomesticSewageAmmoniaNitrogenEntity> list = domesticSewageAmmoniaNitrogenMapper.findAllByShiftId(domesticSewageShiftEntity.getId());
        domesticSewageAmmoniaNitrogenMapper.deleteAll(list);
        if (ammList!=null) {
            for (DomesticSewageAmmoniaNitrogenEntity amm : ammList) {
                amm.setShiftId(domesticSewageShiftEntity.getId());
                domesticSewageAmmoniaNitrogenMapper.save(amm);
            }
        }

    }

    public void delete(Long id){
        sewageIndexMapper.deleteById(id);
    }


    public Map<String,Object> findByMonth(String mineCode, String month) throws ParseException {
        List<DomesticSewageMonthStatistics> list = new ArrayList<>();
        List<BigDecimal> count = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        if (StringUtils.isEmpty(month)){
            month = simpleDateFormat.format(new Date());
        }
        //基础
        List<Map<String,Object>> mapList = domesticSewageAmmoniaNitrogenMapper.findByMonth(month,mineCode);
        //合计
        Map<String,Object> countMapList = domesticSewageAmmoniaNitrogenMapper.findCountMonth(month,mineCode);

        list = Convert.toList(DomesticSewageMonthStatistics.class, mapList);

        Map<String,Object> map = new HashMap<>();
        list = mapToList(list,month);
        map.put("list",list);
        map.put("count",countMapList);
        return map;
    }

    private List<DomesticSewageMonthStatistics> mapToList(List<DomesticSewageMonthStatistics> list,String month) throws ParseException {
        Map<String,DomesticSewageMonthStatistics> map = listToMap(list);
        list = new ArrayList<>();

        month = month +"-01";
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        calendar.setTime(simpleDateFormat.parse(month));


        int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i=0;i<max;i++){
            String time = simpleDateFormat.format(calendar.getTime());
            calendar.add(Calendar.DAY_OF_MONTH,1);
            DomesticSewageMonthStatistics domesticSewageMonthStatistics = map.get(time);
            if (domesticSewageMonthStatistics==null){
                domesticSewageMonthStatistics = new DomesticSewageMonthStatistics();
                domesticSewageMonthStatistics.setTime(time);
            }
            list.add(domesticSewageMonthStatistics);
        }
        return list;
    }

    private Map<String,DomesticSewageMonthStatistics> listToMap(List<DomesticSewageMonthStatistics> list){
        Map<String,DomesticSewageMonthStatistics> map = new HashMap<>();
        for (DomesticSewageMonthStatistics d:list){
            map.put(d.getTime(),d);
        }
        return map;
    }


}
